package com.coollang.smartbra.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.coollang.smartbra.MyAutoLayoutActivity;
import com.coollang.smartbra.R;
import com.coollang.smartbra.adapter.WelcomePagerAdapter;
import com.coollang.smartbra.presenter.Welcome2ActivityPresenter;
import com.coollang.smartbra.utils.ActivitySwitcher;
import com.coollang.smartbra.utils.TranslucentSystemBarUtils;
import com.coollang.smartbra.views.LoginViewPager;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @Auther: Timmy
 * @Data: 2017/7/19
 * @Fuction:
 */
public class Welcome2Activity extends MyAutoLayoutActivity {

    @BindView(R.id.status_bar)
    View statusBar;
    @BindView(R.id.content)
    RelativeLayout content;
    @BindView(R.id.view_pager_welcom)
    LoginViewPager mWelcomViewPager;
    @BindView(R.id.iv_back)
    ImageView mIvBack;

    private WelcomePagerAdapter mPagerAdapter;
    private Welcome2ActivityPresenter mPresenter;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_welcome2);
        ButterKnife.bind(this);
        TranslucentSystemBarUtils.showChenJinZhuangTaiLan(statusBar, this, content);
        initViews();
    }

    private void initViews() {
        mPagerAdapter = new WelcomePagerAdapter(getSupportFragmentManager());
        mWelcomViewPager.setAdapter(mPagerAdapter);
        mWelcomViewPager.setCurrentItem(0);
    }

    @Override
    protected void initData() {
        mPresenter = new Welcome2ActivityPresenter(this);
    }

    public void changePager(int position) {
        mWelcomViewPager.setCurrentItem(position);
        mIvBack.setVisibility(position != 0 ? View.VISIBLE : View.GONE);
    }

    // 修改难度
    public void requestGoal(int goal) {
         mPresenter.requestGoal(goal);
        changePager(1);
    }

    // 修改强度
    public void requestStrength(int strength) {
         mPresenter.requestStrength(strength);
        changePager(2);
    }


    public void requestChangeUserInfo(Map<String, String> map) {
        mPresenter.requestChangeUserInfo(map);
    }


    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        changePager(mWelcomViewPager.getCurrentItem() - 1);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            switch (mWelcomViewPager.getCurrentItem()) {
                case 0:
                    return super.onKeyDown(keyCode, event);
                case 1:
                    changePager(0);
                    return true;
                case 2:
                    changePager(1);
                    return true;
                default:
                    return true;
            }
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    public void setSucceed() {
        ActivitySwitcher.goMainActivity(this);
        finish();
    }
}
